﻿using System;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace SilverlightTetris_M10CDE
{
    /// <summary>
    /// Helper method to remove Rectangles from grid more efficiently
    /// </summary>
    public static class GridLocate
    {
        /// <summary>
        /// This method is used to reset both the gameboard when the game finishes and the next shape board everytime a new shape is generated the old shape needs to be removed
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        public static List<Rectangle> getAllBlocks(this Grid instance)
        {
            if (null == instance)
            {
                throw new ArgumentNullException("instance");
            }
            List<Rectangle> list = new List<Rectangle>();
            foreach (Rectangle myRectangle in instance.Children)
            {
                list.Add(myRectangle);
            }
            return list;
        }


        /// <summary>
        /// Checks how many blocks are on a certain row of the game.
        /// This is then used by the game logic to decide wheather or not to remove a line of blocks from the grid
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="yPos"></param>
        /// <returns></returns>
        public static List<Rectangle> getLineBlocks(this Grid instance, int yPos)
        {
            if (null == instance)
            {
                throw new ArgumentNullException("instance");
            }
            List<Rectangle> list = new List<Rectangle>();
            foreach (Rectangle myRectangle in instance.Children)
            {
                if (Grid.GetRow(myRectangle) == yPos)
                {
                    list.Add(myRectangle);
                }
            }
            return list;
        }

        /// <summary>
        /// Gets all rectangle objects from grid except the ones from a specific line.
        /// This is to get all the other blocks the grid (excluding the ones to be removed) so that they can be referenced from the game logic and their position on the grid decreased by 1
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="yPos"></param>
        /// <returns></returns>
        public static List<Rectangle> getAllOtherLineBlocks(this Grid instance, int yPos)
        {
            if (null == instance)
            {
                throw new ArgumentNullException("instance");
            }
            List<Rectangle> list = new List<Rectangle>();
            foreach (Rectangle myRectangle in instance.Children)
            {
                if (Grid.GetRow(myRectangle) != yPos)
                {
                    list.Add(myRectangle);
                }
            }
            return list;
        }
    }
}
